﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Telerik.Windows.Data;
using Telerik.Windows.Controls;

namespace Rio.Framework.Pages.Workflow.Models
{
    [ContentProperty("Children")]
    public class DataItemTreeView : ViewModelBase
    {
        #region Fields

        private bool m_bIsDropAllowed;
        private bool m_bIsExpanded;
        private bool m_bIsSelected;
        private QueryableCollectionView m_CollectionMessage;
        private int m_iIdRepertory;
        private DataItemTreeViewCollection m_Items;
        private FontWeight m_oTextFont = FontWeights.Normal;
        private DataItemTreeView m_Parent;
        private string m_sImageSrc;
        private long m_sNumberNewItem;
        private long m_sNumberItem;
        private string m_sText;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DataItemTreeView" /> class.
        /// </summary>
        public DataItemTreeView()
        {
            this.m_Items = new DataItemTreeViewCollection(this);
            this.m_CollectionMessage = new QueryableCollectionView(new List<Message>());
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets the use workflow title format.
        /// </summary>
        /// <value>The use workflow title format.</value>
        public bool UseWorkflowTitleFormat { get; set; }

        /// <summary>
        /// Gets or sets the b is drop allowed.
        /// </summary>
        /// <value>The b is drop allowed.</value>
        public bool bIsDropAllowed
        {
            get
            {
                return this.m_bIsDropAllowed;
            }
            set
            {
                if (this.m_bIsDropAllowed != value)
                {
                    this.m_bIsDropAllowed = value;
                    this.OnPropertyChanged("bIsDropAllowed");
                }
            }
        }

        /// <summary>
        /// Gets or sets the b is expanded.
        /// </summary>
        /// <value>The b is expanded.</value>
        public bool bIsExpanded
        {
            get
            {
                return this.m_bIsExpanded;
            }
            set
            {
                if (this.m_bIsExpanded != value)
                {
                    this.m_bIsExpanded = value;
                    this.OnPropertyChanged("bIsExpanded");
                }
            }
        }

        /// <summary>
        /// Gets or sets the b is selected.
        /// </summary>
        /// <value>The b is selected.</value>
        public bool bIsSelected
        {
            get
            {
                return this.m_bIsSelected;
            }
            set
            {
                if (this.m_bIsSelected != value)
                {
                    this.m_bIsSelected = value;
                    this.OnPropertyChanged("bIsSelected");
                }
            }
        }

        /// <summary>
        /// Gets or sets the collection message.
        /// </summary>
        /// <value>The collection message.</value>
        public QueryableCollectionView CollectionMessage
        {
            get
            {
                return this.m_CollectionMessage;
            }
            set
            {
                if (this.m_CollectionMessage != value)
                {
                    this.m_CollectionMessage = value;
                    this.OnPropertyChanged("CollectionMessage");
                }
            }
        }

        /// <summary>
        /// Gets or sets the folder type id.
        /// </summary>
        /// <value>The folder type id.</value>
        public long FolderTypeId { get; set; }

        /// <summary>
        /// Gets or sets the i id repertory.
        /// </summary>
        /// <value>The i id repertory.</value>
        public int iIdRepertory
        {
            get
            {
                return this.m_iIdRepertory;
            }
            set
            {
                if (this.m_iIdRepertory != value)
                {
                    this.m_iIdRepertory = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the items.
        /// </summary>
        /// <value>The items.</value>
        public DataItemTreeViewCollection Items
        {
            get
            {
                return this.m_Items;
            }

            set
            {
                if (this.m_Items != value)
                {
                    this.m_Items = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the type of the item.
        /// </summary>
        /// <value>The type of the item.</value>
        public WorkflowTreeItemType ItemType { get; set; }

        /// <summary>
        /// Gets or sets the number new item.
        /// </summary>
        /// <value>The number new item.</value>
        public long NumberNewItem
        {
            get
            {
                return this.m_sNumberNewItem;
            }
            set
            {
                this.m_sNumberNewItem = value;
                this.OnPropertyChanged("NumberNewItem");

                FormatTitle();
            }
        }

        /// <summary>
        /// Gets or sets the number item.
        /// </summary>
        /// <value>The number item.</value>
        public long NumberItem
        {
            get
            {
                return this.m_sNumberItem;
            }
            set
            {
                this.m_sNumberItem = value;
                this.OnPropertyChanged("NumberItem");

                FormatTitle();
            }
        }

        /// <summary>
        /// Gets or sets the organization id.
        /// </summary>
        /// <value>The organization id.</value>
        public long OrganizationId { get; set; }

        /// <summary>
        /// Gets or sets the parent.
        /// </summary>
        /// <value>The parent.</value>
        public DataItemTreeView Parent
        {
            get
            {
                return this.m_Parent;
            }
            set
            {
                this.m_Parent = value;
            }
        }

        /// <summary>
        /// Gets or sets the s image SRC.
        /// </summary>
        /// <value>The s image SRC.</value>
        public string sImageSrc
        {
            get
            {
                return this.m_sImageSrc;
            }
            set
            {
                this.m_sImageSrc = value;
            }
        }

        /// <summary>
        /// Gets or sets the s text.
        /// </summary>
        /// <value>The s text.</value>
        public string sText
        {
            get
            {
                return m_sText;
            }
            set
            {
                if (this.m_sText != value)
                {
                    this.m_sText = value;
                    this.OnPropertyChanged("sText");
                }
            }
        }

        /// <summary>
        /// Gets or sets the type of the sub item.
        /// </summary>
        /// <value>The type of the sub item.</value>
        public WorkflowTreeSubItemType SubItemType { get; set; }

        /// <summary>
        /// Gets or sets the text font.
        /// </summary>
        /// <value>The text font.</value>
        public FontWeight TextFont
        {
            get
            {
                return this.m_oTextFont;
            }
            set
            {
                m_oTextFont = value;
                this.OnPropertyChanged("TextFont");
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Formats the title.
        /// </summary>
        private void FormatTitle()
        {
            string[] l_sTextArray = m_sText.Split('(');

            if (m_sNumberNewItem > 0)
            {
                sText = String.Format("{0}({1}/{2})", l_sTextArray[0], m_sNumberNewItem, m_sNumberItem);

                TextFont = FontWeights.Bold;
            }
            else
            {
                if (m_sNumberItem > 0)
                {
                    sText = String.Format("{0}({1})", l_sTextArray[0], m_sNumberItem);
                }
                else
                {
                    sText = String.Format("{0}", l_sTextArray[0]);
                }

                TextFont = FontWeights.Normal;
            }
        }

        #endregion Methods
    }
}